/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit.tkorg.and.gui;

import java.awt.Frame;
import java.io.File;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import uit.tkorg.and.models.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import org.apache.commons.math3.stat.descriptive.moment.Mean;
import org.encog.Encog;
import org.encog.app.analyst.EncogAnalyst;
import org.encog.engine.network.activation.ActivationElliottSymmetric;
import org.encog.engine.network.activation.ActivationRamp;
import org.encog.engine.network.activation.ActivationSigmoid;
import org.encog.engine.network.activation.ActivationSoftMax;
import org.encog.ensemble.bagging.Bagging;
import org.encog.ml.data.MLData;
import org.encog.ml.data.MLDataPair;
import org.encog.ml.data.MLDataSet;
import org.encog.ml.data.basic.BasicMLDataSet;
import org.encog.ml.train.strategy.end.EarlyStoppingStrategy;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.layers.BasicLayer;
import org.encog.neural.networks.training.propagation.resilient.ResilientPropagation;
import org.encog.neural.networks.training.strategy.RegularizationStrategy;
import org.encog.util.EncogValidate;
import org.encog.util.benchmark.EncogBenchmark;
import org.encog.util.simple.EncogUtility;
import uit.tkorg.and.core.classifications.DNN;
import uit.tkorg.and.core.classifications.WekaDNN;
import uit.tkorg.and.models.Feature;
import uit.tkorg.and.models.MachineLearning;
import uit.tkorg.and.models.PairPublication;
import weka.classifiers.Evaluation;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Utils;

/**
 *
 * @author tiendv
 */
public class Main extends javax.swing.JFrame {
    private PairPublication[] pairTest = null;
    private PairPublication[] pairTrain = null;

    private Instances train = null;
    private Instances test = null;

    private double AND_INPUT_Train[][] = null;
    private double AND_Label_Train[][] = null;
    private double AND_INPUT_Test[][] = null;
    private double AND_Label_Test[][] = null;
    
    private MLDataSet trainingSet = null;
    private MLDataSet testSet = null;

    int numHiddenLayer = 0;
    int numHiddenUnit = 0;

    /**
     * Creates new form Main
     */
    public Main() {
        initComponents();
        this.setExtendedState(JFrame.NORMAL);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfTrainingDataParth = new javax.swing.JTextField();
        tfTestDataParth = new javax.swing.JTextField();
        btTrainingDataParth = new javax.swing.JButton();
        btTestDataParth = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        cbJaccardAuthorName = new javax.swing.JCheckBox();
        cbJaccardAfiliation = new javax.swing.JCheckBox();
        cbJaccardCoAuthor = new javax.swing.JCheckBox();
        cbJaccardKeyword = new javax.swing.JCheckBox();
        cbJaccardKeyInteresting = new javax.swing.JCheckBox();
        cbLevenshteinAuthorName = new javax.swing.JCheckBox();
        cbLevenshteinAfflicaiton = new javax.swing.JCheckBox();
        cbJaroAuthorName = new javax.swing.JCheckBox();
        cbJarodAfiliation = new javax.swing.JCheckBox();
        cbJaroWinklerAuthorName = new javax.swing.JCheckBox();
        cbJaroWinklerAffiliaiton = new javax.swing.JCheckBox();
        cbSmithWatermanAuthorName = new javax.swing.JCheckBox();
        cbSmithWatermanAffiliation = new javax.swing.JCheckBox();
        cbMongeElkanAuthorName = new javax.swing.JCheckBox();
        cbMongeElkanAffiliaiton = new javax.swing.JCheckBox();
        jToggleButton1 = new javax.swing.JToggleButton();
        jPanel3 = new javax.swing.JPanel();
        rbRF = new javax.swing.JRadioButton();
        rbSVM = new javax.swing.JRadioButton();
        rbBayes = new javax.swing.JRadioButton();
        rbc45 = new javax.swing.JRadioButton();
        rdKNN = new javax.swing.JRadioButton();
        btRun = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taLog = new javax.swing.JTextArea();
        jRadioButton1 = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        tfNumHiddenLayer = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfNumHiddenUnit = new javax.swing.JTextField();
        jRadioButton2 = new javax.swing.JRadioButton();
        jbClearLog = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Data"));

        jLabel1.setText("Dateset Folder:");

        jLabel2.setText("Test Data");

        btTrainingDataParth.setText("Open");
        btTrainingDataParth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTrainingDataParthActionPerformed(evt);
            }
        });

        btTestDataParth.setText("Open");
        btTestDataParth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTestDataParthActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tfTestDataParth)
                    .addComponent(tfTrainingDataParth, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btTrainingDataParth)
                    .addComponent(btTestDataParth))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfTrainingDataParth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btTrainingDataParth))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfTestDataParth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btTestDataParth))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Features"));

        cbJaccardAuthorName.setText("JCAuthorName");

        cbJaccardAfiliation.setText("JCAffiliation ");

        cbJaccardCoAuthor.setText("JCCoAuthor");

        cbJaccardKeyword.setText("JCKeyWord");

        cbJaccardKeyInteresting.setText("JCKeyInteresting");

        cbLevenshteinAuthorName.setText("LevenshteinAuthorName");
        cbLevenshteinAuthorName.setToolTipText("");

        cbLevenshteinAfflicaiton.setText("LevenshteinAffiliation");

        cbJaroAuthorName.setText("JaroAuthorName");

        cbJarodAfiliation.setText("JaroAffiliation ");

        cbJaroWinklerAuthorName.setText("JaroWinklerAuthorName");

        cbJaroWinklerAffiliaiton.setText("JaroWinklerAffiliaiton");

        cbSmithWatermanAuthorName.setText("SmithWatermanAuthorName");

        cbSmithWatermanAffiliation.setText("SmithWatermanAffiliation");

        cbMongeElkanAuthorName.setText("MongeElkanAuthorName");

        cbMongeElkanAffiliaiton.setText("MongeElkanAffiliaiton");

        jToggleButton1.setText("Toggle check all");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbJaccardAuthorName)
                    .addComponent(cbJaroWinklerAffiliaiton)
                    .addComponent(cbJaroWinklerAuthorName)
                    .addComponent(cbJarodAfiliation)
                    .addComponent(cbJaroAuthorName)
                    .addComponent(cbLevenshteinAfflicaiton)
                    .addComponent(cbLevenshteinAuthorName)
                    .addComponent(cbJaccardKeyInteresting)
                    .addComponent(cbJaccardKeyword)
                    .addComponent(cbJaccardCoAuthor)
                    .addComponent(cbJaccardAfiliation))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbSmithWatermanAuthorName)
                    .addComponent(cbSmithWatermanAffiliation)
                    .addComponent(cbMongeElkanAffiliaiton)
                    .addComponent(cbMongeElkanAuthorName)
                    .addComponent(jToggleButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbJaccardAuthorName)
                    .addComponent(cbSmithWatermanAuthorName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbJaccardAfiliation)
                    .addComponent(cbSmithWatermanAffiliation))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbJaccardCoAuthor)
                    .addComponent(cbMongeElkanAuthorName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbJaccardKeyword)
                    .addComponent(cbMongeElkanAffiliaiton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbJaccardKeyInteresting)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbLevenshteinAuthorName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbLevenshteinAfflicaiton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbJaroAuthorName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbJarodAfiliation)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(cbJaroWinklerAuthorName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbJaroWinklerAffiliaiton)
                        .addContainerGap(14, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jToggleButton1)
                        .addGap(19, 19, 19))))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Classifiers"));

        buttonGroup1.add(rbRF);
        rbRF.setText("Random Forest");

        buttonGroup1.add(rbSVM);
        rbSVM.setText("SVM");

        buttonGroup1.add(rbBayes);
        rbBayes.setText("Bayes");

        buttonGroup1.add(rbc45);
        rbc45.setText("C4.5");

        buttonGroup1.add(rdKNN);
        rdKNN.setText("KNN");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rbRF)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rbSVM)
                .addGap(10, 10, 10)
                .addComponent(rbBayes)
                .addGap(18, 18, 18)
                .addComponent(rbc45)
                .addGap(18, 18, 18)
                .addComponent(rdKNN)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbRF)
                    .addComponent(rbSVM)
                    .addComponent(rbBayes)
                    .addComponent(rbc45)
                    .addComponent(rdKNN))
                .addContainerGap(9, Short.MAX_VALUE))
        );

        btRun.setText("RUN");
        btRun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRunActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Log"));

        taLog.setColumns(20);
        taLog.setRows(5);
        jScrollPane1.setViewportView(taLog);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("DNN");

        jLabel3.setText("#Hidden layer:");

        tfNumHiddenLayer.setText("1");

        jLabel4.setText("#Hidden unit:");

        tfNumHiddenUnit.setText("25");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("WekaDNN");

        jbClearLog.setText("Clear log");
        jbClearLog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbClearLogActionPerformed(evt);
            }
        });

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(21, 21, 21))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButton1)
                                .addGap(28, 28, 28)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tfNumHiddenLayer, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel4))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButton2)
                                .addGap(48, 48, 48)
                                .addComponent(btRun, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfNumHiddenUnit, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbClearLog))
                        .addGap(33, 33, 33)))
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(14, 14, 14))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4)
                                .addComponent(tfNumHiddenUnit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(tfNumHiddenLayer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jRadioButton1))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(25, 25, 25)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(btRun, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jbClearLog)))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jRadioButton2)))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btTrainingDataParthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTrainingDataParthActionPerformed
        // TODO add your handling code here:
        

        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        fc.showOpenDialog(this);  
        // Show open dialog; this method does not return until the dialog is closed       
        tfTrainingDataParth.setText(fc.getSelectedFile().getAbsolutePath());
    }//GEN-LAST:event_btTrainingDataParthActionPerformed

    private void btTestDataParthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTestDataParthActionPerformed
        // TODO add your handling code here:
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        fc.showOpenDialog(this);  
        // Show open dialog; this method does not return until the dialog is closed       
        tfTestDataParth.setText(fc.getSelectedFile().getAbsolutePath());
    }//GEN-LAST:event_btTestDataParthActionPerformed

    private void btRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRunActionPerformed
        try {                                      
                Main.taLog.append("Reading option...\n");

                // TODO add your handling code here:
                // Get path data

                String pathForTrain = tfTrainingDataParth.getText();
                String pathForTest = tfTestDataParth.getText();

                //Get Feature

                Feature selectFeatures = new Feature();
                int dimension =0;

                // Jaccard Authorname
                if(cbJaccardAuthorName.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setJcAuthorName(true);
                }
                // Jaccard Coauthor 
                if(cbJaccardCoAuthor.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setJcCoAuthor(true);
                }
                  // Jaccard Afiliation
                if(cbJaccardAfiliation.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setJcAffiliation(true);
                }
                  // Jaccard keword
                if(cbJaccardKeyword.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setJcKeyword(true);
                }
                  // Jaccard Interesting
                if(cbJaccardKeyInteresting.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setJcInterestingKeyword(true);
                }

                  // Levenshtein Author name
                if(cbLevenshteinAuthorName.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setLevenshteinAuthorname(true);
                }
                  // Levenshtein Affilication
                if(cbLevenshteinAfflicaiton.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setLevenshteinAffiliaiton(true);
                }    
                // Jaro Author Name
                if(cbJaroAuthorName.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setJaroAuthorName(true);
                }            
                // Jarro Affilication
                if(cbJarodAfiliation.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setJaroAffiliation(true);
                }  
                // Jarro Winkler AuthorName
                if(cbJaroWinklerAuthorName.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setJarowinklerAuthorName(true);
                }
                 // Jarro Winkler Affiliaction 
                if(cbJaroWinklerAffiliaiton.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setJarowinklerAffiliaiton(true);
                }
                // SmithWaterman AuthorName
                if(cbSmithWatermanAuthorName.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setSmithWatermanAuthorName(true);
                }
               // SmithWaterman Author Affiliaiton
                if(cbSmithWatermanAffiliation.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setSmithWatermanAffiliation(true);
                }
               // Monge-Elkan Author Name
                if(cbMongeElkanAuthorName.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setMongeElkanAuthorName(true);
                }
                // Monge-Elkan Author Affiliation
                if(cbMongeElkanAffiliaiton.isSelected())
                {
                    dimension ++;
                    selectFeatures.setNumberSelectFeature(dimension);
                    selectFeatures.setMongeElkanAffiliation(true);
                }

                // Add more feature here   

                // Get Classifier
                JRadioButton nameClassifier = getSelection(buttonGroup1);
                String name = nameClassifier.getText();
                Main.taLog.append("=============================================\n");
                Main.taLog.append("Classifier: " + name);
                Main.taLog.append("\n");
                MachineLearning mc = null;
                if(name.equals("Random Forest"))
                    mc = new MachineLearning(MachineLearning.TypeClassifier.RF);
                if(name.equals("SVM"))
                    mc = new MachineLearning(MachineLearning.TypeClassifier.SVM);
                if(name.equals("Bayes"))
                    mc = new MachineLearning(MachineLearning.TypeClassifier.BY);
                if(name.equals("C4.5"))
                    mc = new MachineLearning(MachineLearning.TypeClassifier.C45);
                if(name.equals("KNN"))
                    mc = new MachineLearning(MachineLearning.TypeClassifier.KNN);
            
//------------- Prepare data ---------------------------------------------------
                if (train == null) {
                    Main.taLog.append("Preparing Data ...\n");

                    if (pathForTrain == null || pathForTrain.isEmpty())
//                        pathForTrain = "C:\\VANDData\\TrainData";
                        pathForTrain = "E:\\ResE\\DNN\\Data\\TrainData";
                    if (pathForTest == null || pathForTest.isEmpty())
//                        pathForTest = "C:\\VANDData\\TestData";
                        pathForTest = "E:\\ResE\\DNN\\Data\\TestData";

                    pairTest = Vector.buildVectorsFromFolderPairPublication(pathForTest);    
                    pairTrain = Vector.buildVectorsFromFolderPairPublication(pathForTrain);   

                    test = Vector.buildVector(pairTest, selectFeatures);
                    train = Vector.buildVector(pairTrain, selectFeatures);
                }
//------------- Prepare data ---------------------------------------------------                

                /**
                 *  DNN option.
                 * 
                 */
                if(name.equals("DNN"))
                {
                    // Encog DNN
                    if (AND_INPUT_Train == null) {
                        Main.taLog.append("Preparing Data for DNN ...\n");

                        // Prepare data
                        // For construct
                        AND_INPUT_Train = asArrayInput(train);
                        AND_Label_Train = asArrayLabel(train,dimension);
                        // create training data
                        trainingSet = new BasicMLDataSet(AND_INPUT_Train, AND_Label_Train);

                        // Prepare data
                        // For Test
                        AND_INPUT_Test = asArrayInput(test);
                        AND_Label_Test = asArrayLabel(test,dimension);
                        // create testing data
                        testSet = new BasicMLDataSet(AND_INPUT_Test, AND_Label_Test);
                    }
                    
                    // Get size.
                    numHiddenLayer = Integer.parseInt(tfNumHiddenLayer.getText());
                    numHiddenUnit = Integer.parseInt(tfNumHiddenUnit.getText());
                    
                    // Train DNN
                    Main.taLog.append("Training DNN ...\n");
                    Main.taLog.append(numHiddenLayer + " x " + numHiddenUnit + "\n");

                    DNN dnn = new DNN();
                    dnn.train(trainingSet, testSet, numHiddenLayer, numHiddenUnit);

                    // test the neural network
                    double[] testSetMSE = new double[6];
                    for (int k = 0; k < 5; k++) {
                        testSetMSE[k] = dnn.getNetwork()[k].calculateError(testSet);
                        testSetMSE[5] += testSetMSE[k];
                        System.out.println("Test set MSE Error: " + testSetMSE[k]);
                    }
                    
                    double classificationAccuracy = DNN.calculateAccuracy(dnn.getNetwork(), testSet);
                    System.out.println("Test set Classification Accuracy: " + classificationAccuracy);
                    System.out.println("Test set Classification Error rate: " + (1 - classificationAccuracy));
                    
                    Main.taLog.append("Result : ");
                    Main.taLog.append("\n");
                    Main.taLog.append("Test set MSE Error: " + testSetMSE[5]/5);
                    Main.taLog.append("\n");
                    Main.taLog.append("Test set Classification Accuracy: " + classificationAccuracy);
                    Main.taLog.append("\n");
                    Main.taLog.append("Test set Classification Error rate: " + (1 - classificationAccuracy));
                    Main.taLog.append("\n");

                } else if (name.equals("WekaDNN")) {
                    // Weka bi loi, chi dua vao de test, khong dung duoc.
                    Main.taLog.append("Building classifier ...");
                    Main.taLog.append("\n");

                    // Get size.
                    numHiddenLayer = Integer.parseInt(tfNumHiddenLayer.getText());
                    numHiddenUnit = Integer.parseInt(tfNumHiddenUnit.getText());
                    Main.taLog.append(numHiddenLayer + " x " + numHiddenUnit + "\n");
                    
                    WekaDNN wekaDNN = new WekaDNN();
                    wekaDNN.construct(train, test, numHiddenLayer, numHiddenUnit);
                    wekaDNN.getNetwork().buildClassifier(train);
                    
                    Evaluation eTest = new Evaluation(test);
                    eTest.evaluateModel(wekaDNN.getNetwork(), test);

                    System.out.println("Result : ");                    
                    System.out.println(eTest.toMatrixString());
                    System.out.println(eTest.toSummaryString());  

                    Main.taLog.append("Result : ");
                    Main.taLog.append("\n");
                    Main.taLog.append(eTest.toMatrixString());
                    Main.taLog.append("\n");
                    Main.taLog.append(eTest.toSummaryString());
                    Main.taLog.append("Total Number of Instance For Train : " + train.numInstances());
                    Main.taLog.append("\n");
                }
                else
                {
                    Main.taLog.append("Training ...\n");

                    mc.cModel.buildClassifier(train);
                    Evaluation eTest = new Evaluation(test);
                    eTest.evaluateModel(mc.cModel, test);

                    System.out.println("Result : ");                    
                    System.out.println(eTest.toMatrixString());
                    System.out.println(eTest.toSummaryString());  

                    Main.taLog.append("Result : ");
                    Main.taLog.append("\n");
                    Main.taLog.append(eTest.toMatrixString());
                    Main.taLog.append("\n");
                    Main.taLog.append(eTest.toSummaryString());
                    Main.taLog.append("Total Number of Instance For Train : " + train.numInstances());
                    Main.taLog.append("\n");
                }
                
//                test = null;
//                construct = null;
//
//                pairTest.clone();
//                pairTrain.clone();
         } catch (Exception ex) {
            taLog.append(Main.class.getName() + " -EXCEPTION: " + ex.getMessage());
            ex.printStackTrace();
        }         
    }//GEN-LAST:event_btRunActionPerformed

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        cbJaccardAuthorName.doClick();
        cbJaccardAfiliation.doClick();
        cbJaccardCoAuthor.doClick();
        cbJaccardKeyInteresting.doClick();
        cbJaccardKeyword.doClick();
        cbJaroAuthorName.doClick();
        cbJaroWinklerAffiliaiton.doClick();
        cbJaroWinklerAuthorName.doClick();
        cbJarodAfiliation.doClick();
        cbLevenshteinAfflicaiton.doClick();
        cbLevenshteinAuthorName.doClick();
        cbMongeElkanAffiliaiton.doClick();
        cbMongeElkanAuthorName.doClick();
        cbSmithWatermanAffiliation.doClick();
        cbSmithWatermanAuthorName.doClick();
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jbClearLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbClearLogActionPerformed
        // TODO add your handling code here:
        Main.taLog.setText(null);
    }//GEN-LAST:event_jbClearLogActionPerformed

    public static double[][] asArrayInput(Instances data) {
        double Data[][] = new double[data.numInstances()][data.numAttributes()-1];
        for (int i = 0; i < data.numInstances(); i++) {
            for (int j = 0; j < data.numAttributes()-1; j++) {
                Data[i][j] = data.instance(i).value(j);
                // Standardize data.
                if (Double.isNaN(Data[i][j])) {
                    Data[i][j] = 0;
                }
                else if (Double.isInfinite(Data[i][j])) {
                    Data[i][j] = 1;
                }
            }
        }
        return Data;
      } 
    public static double[][] asArrayLabel(Instances data, int dimension) {
        // prob same, prob diff.
        // 2 class -> 1 unit logistic.
        // softmax require all units sum to 1, or it will produce NaN.
        // sigmoid is special 1 unit softmax with 2 class, try sigmoid.
//        double Data[][] = new double[data.numInstances()][2];
        double Data[][] = new double[data.numInstances()][1];
        
        for (int i = 0; i < data.numInstances(); i++) {
            Data[i][0] = 1 - data.instance(i).value(dimension);
//            Data[i][1] = data.instance(i).value(dimension);
            // Standardize data.
            if (Double.isNaN(Data[i][0])) {
                Data[i][0] = 0;
            }
            else if (Double.isInfinite(Data[i][0])) {
                Data[i][0] = 1;
            }
            else if (Data[i][0] < 0.5) {
                Data[i][0] = 0;
            }
            else if (Data[i][0] >= 0.5) {
                Data[i][0] = 1;
            }
        }
        return Data;
      }
        
    public PairPublication[] doubleListPairPublication(PairPublication[] pairList)
    {
        PairPublication[] list = new PairPublication[pairList.length * 2];        
        for(int i = 0; i < pairList.length; i++)
        {
            list[i] = pairList[i];
            list[i + pairList.length] = pairList[i];
        }        
        return list;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    public static JRadioButton getSelection(ButtonGroup group) {
    for (Enumeration e=group.getElements(); e.hasMoreElements(); ) {
        JRadioButton b = (JRadioButton)e.nextElement();
        if (b.getModel() == group.getSelection()) {
            return b;
        }
    }
    return null;
}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btRun;
    private javax.swing.JButton btTestDataParth;
    private javax.swing.JButton btTrainingDataParth;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JCheckBox cbJaccardAfiliation;
    private javax.swing.JCheckBox cbJaccardAuthorName;
    private javax.swing.JCheckBox cbJaccardCoAuthor;
    private javax.swing.JCheckBox cbJaccardKeyInteresting;
    private javax.swing.JCheckBox cbJaccardKeyword;
    private javax.swing.JCheckBox cbJaroAuthorName;
    private javax.swing.JCheckBox cbJaroWinklerAffiliaiton;
    private javax.swing.JCheckBox cbJaroWinklerAuthorName;
    private javax.swing.JCheckBox cbJarodAfiliation;
    private javax.swing.JCheckBox cbLevenshteinAfflicaiton;
    private javax.swing.JCheckBox cbLevenshteinAuthorName;
    private javax.swing.JCheckBox cbMongeElkanAffiliaiton;
    private javax.swing.JCheckBox cbMongeElkanAuthorName;
    private javax.swing.JCheckBox cbSmithWatermanAffiliation;
    private javax.swing.JCheckBox cbSmithWatermanAuthorName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JButton jbClearLog;
    private javax.swing.JRadioButton rbBayes;
    private javax.swing.JRadioButton rbRF;
    private javax.swing.JRadioButton rbSVM;
    private javax.swing.JRadioButton rbc45;
    private javax.swing.JRadioButton rdKNN;
    public static javax.swing.JTextArea taLog;
    private javax.swing.JTextField tfNumHiddenLayer;
    private javax.swing.JTextField tfNumHiddenUnit;
    private javax.swing.JTextField tfTestDataParth;
    private javax.swing.JTextField tfTrainingDataParth;
    // End of variables declaration//GEN-END:variables
}
